package com.ruoyi.test;

import cn.hutool.core.util.ObjectUtil;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public class ExactAllCommonSeq {

    /**
     * 求取两个字段所有的子序列
     * @param inputA
     * @param inputB
     * @return
     */
    public static List<String> exactAllCommonSeq(String inputA, String inputB) {
        //把字符串转成字符数组
        char[] arr1 = inputA.toCharArray();
        char[] arr2 = inputB.toCharArray();
        // 把两个字符串分别以行和列组成一个二维矩阵
        int[][] temp = new int[arr1.length][arr2.length];
        //初始化二维矩阵中的第一行
        for (int i = 0; i < arr2.length; i++) {
            temp[0][i] = (arr1[0] == arr2[i]) ? 1 : 0;
        }
        //初始化二维矩阵中的第一列
        for (int j = 0; j < arr1.length; j++) {
            temp[j][0] = (arr2[0] == arr1[j]) ? 1 : 0;
        }
        //嵌套for循环：比较二维矩阵中每个点对应行列字符中否相等，相等的话值设置为1，否则设置为0
        for (int i = 1; i < arr1.length; i++) {
            for (int j = 1; j < arr2.length; j++) {
                if (arr1[i] == arr2[j]) {
                    //对角线上一个值加1，方便求取每个公共子序列的长度
                    temp[i][j] = temp[i - 1][j - 1] + 1;
                } else {
                    temp[i][j] = 0;
                }
            }
        }

        List<Map<String, Integer>> list = new ArrayList<>();
        //依次遍历对角矩阵的对角线
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                Map<String, Integer> map = new HashMap<>();
                if (temp[i][j] != 0 && temp[i][j] != 1) {
                    if (temp[i - 1][j - 1] == temp[i][j] - 1) {
                        if (i - 1 > 0 && j - 1 > 0 && i + 1 < arr1.length && j + 1 < arr2.length && temp[i - 1][j - 1] != 0 && temp[i + 1][j + 1] == 0) {
                            map.put("row", i);
                            map.put("column", j);
                            map.put("length", temp[i][j]);
                            list.add(map);
                        } else if ((i + 1 == arr1.length || j + 1 == arr2.length)) {
                            map.put("row", i);
                            map.put("column", j);
                            map.put("length", temp[i][j]);
                            list.add(map);
                        }
                    }
                }
            }
        }

        List<String> resultList = new ArrayList<>();
        if (!list.isEmpty()) {
            for (Map<String, Integer> map : list) {
                String s = getsubString(inputA, map.get("row"), map.get("length"));
                resultList.add(s);
            }
        }
        return resultList;
    }

    /**
     * 根据坐标位置及子串长度获取子串内容
     * @param s
     * @param a
     * @param b
     * @return
     */
    public static String getsubString(String s, int a, int b) {
        String s1 ;
        s1 = s.substring(a - b + 1, a + 1);
        return s1;
    }

    public static Set<String> longestCommonSubstrings(String s, String t) {
        int[][] table = new int[s.length()][t.length()];
        int longest = 0;
        Set<String> result = new HashSet<>();

        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < t.length(); j++) {
                if (s.charAt(i) != t.charAt(j)) {
                    continue;
                }

                table[i][j] = (i == 0 || j == 0) ? 1
                    : 1 + table[i - 1][j - 1];
                if (table[i][j] > longest) {
                    longest = table[i][j];
                    result.clear();
                }
                if (table[i][j] == longest) {
                    result.add(s.substring(i - longest + 1, i + 1));
                }
            }
        }
        return result;
    }
    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) {
        try {
            InputStream inputStream = Files.newInputStream(Paths.get("D:\\dev\\RuoYi-Vue-Plus\\ruoyi-admin\\src\\main\\resources\\qqqq.pdf"));
            PdfReader reader = new PdfReader(inputStream);
            int n = reader.getNumberOfPages();
            for (int i = 1; i<= n; i++) {
                String textPage = PdfTextExtractor.getTextFromPage(reader, i);
                int i1 = textPage.lastIndexOf("\n");
                int i2 = textPage.indexOf("\n");
                int length = textPage.length();
                System.out.println(i1);
                System.out.println(length);
                System.out.println(i2);
                System.out.println(textPage);
            }
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
//        try {
//            PDDocument document = PDDocument.load(new File("D:\\dev\\RuoYi-Vue-Plus\\ruoyi-admin\\src\\main\\resources\\临沂市财金大数据有限公司.pdf"));
//            // 获取PDF文件的内容
//            String content = new PDFTextStripper().getText(document);
//            if (ObjectUtil.isEmpty(content)){
//                throw new EmptyStackException();
//            }
//            System.out.println(content);
//            // 关闭文档
//            document.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        //assert value != null;
        //List<String> list = exactAllCommonSeq(value, value1);
        //Set<String> strings = longestCommonSubstrings(value, value1);
        //System.out.println(strings);
        //System.out.println(list);
    }

    private static String extracted() {
        String value = null;
        String value1 = null;
        try {
            // 加载PDF文件
            PDDocument document = PDDocument.load(new File("D:\\dev\\RuoYi-Vue-Plus\\ruoyi-admin\\src\\main\\resources\\山东奥辰建设工程集团有限公司投标文件-.pdf"));
            PDDocument document1 = PDDocument.load(new File("D:\\dev\\RuoYi-Vue-Plus\\ruoyi-admin\\src\\main\\resources\\qqqq.pdf"));
            // 获取PDF文件的内容
            String content = new PDFTextStripper().getText(document);
            if (ObjectUtil.isEmpty(content)){
                throw new EmptyStackException();
            }
            // 获取PDF文件的内容
            String content1 = new PDFTextStripper().getText(document1);
            if (ObjectUtil.isEmpty(content)){
                throw new EmptyStackException();
            }
            //处理字符串只要汉字数字字母
            value = content.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
            value1 = content1.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "");
            // 打印内容
            System.out.println(value);
            System.out.println(value1);

            // 关闭文档
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return value;
    }
}
